package com.example.good.utils;

import com.example.good.enums.ResultEnum;

import java.io.Serializable;

/**
 * @author: Wang Mingjun
 * @date: 2021/1/27
 * @desc:
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -6520802754151061360L;
    private Integer code;
    private String msg;
    private T data;

    public static Result ok() {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("SUCCESS");
        return result;
    }

    public static <T> Result<T> ok(T obj) {
        Result<T> r = new Result();
        r.setCode(ResultEnum.SUCCESS.getCode());
        r.setData(obj);
        return r;
    }

    public static <T> Result<T> error(String msg) {
        return error(ResultEnum.ERROR.getCode(), msg);
    }

    public static <T> Result<T> error(ResultEnum errorEnum) {
        return error(errorEnum.getCode(), errorEnum.getMsg());
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> r = new Result();
        r.setMsg(msg);
        r.setCode(code);
        r.setData(null);
        return r;
    }

    public static <T> Result<T> error(int code, T data, String msg) {
        Result<T> r = new Result();
        r.setMsg(msg);
        r.setData(data);
        r.setCode(code);
        return r;
    }

    public static  <T> Result<T> error(T data) {
        Result<T> r = new Result();
        r.setMsg("FAIL");
        r.setData(data);
        r.setCode(ResultEnum.ERROR.getCode());
        return r;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }


    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
